package com.dm.service;

import com.dm.common.Constants;
import com.dm.common.EmptyUtils;
import com.dm.dao.DmBaseImageMapper;
import com.dm.pojo.DmBaseImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created by dm
 */
@RestController
public class RestDmBaseImageService {

     @Autowired
     private DmBaseImageMapper dmBaseImageMapper;

     @RequestMapping(value = "/getDmBaseImageById",method = RequestMethod.POST)
     public DmBaseImage getDmBaseImageById(@RequestParam("id") Long id)throws Exception{
        return dmBaseImageMapper.getDmBaseImageById(id);
     }

     @RequestMapping(value = "/getDmBaseImageListByMap",method = RequestMethod.POST)
     public List<DmBaseImage>	getDmBaseImageListByMap(@RequestParam Map<String,Object> param)throws Exception{
        return dmBaseImageMapper.getDmBaseImageListByMap(param);
     }

     @RequestMapping(value = "/getDmBaseImageCountByMap",method = RequestMethod.POST)
     public Integer getDmBaseImageCountByMap(@RequestParam Map<String,Object> param)throws Exception{
        return dmBaseImageMapper.getDmBaseImageCountByMap(param);
     }

     @RequestMapping(value = "/qdtxAddDmBaseImage",method = RequestMethod.POST)
     public Integer qdtxAddDmBaseImage(@RequestBody DmBaseImage dmBaseImage)throws Exception{
        dmBaseImage.setCreatedTime(new Date());
        return dmBaseImageMapper.insertDmBaseImage(dmBaseImage);
     }

     @RequestMapping(value = "/qdtxModifyDmBaseImage",method = RequestMethod.POST)
     public Integer qdtxModifyDmBaseImage(@RequestBody DmBaseImage dmBaseImage)throws Exception{
        dmBaseImage.setUpdatedTime(new Date());
        return dmBaseImageMapper.updateDmBaseImage(dmBaseImage);
     }

    @RequestMapping(value = "/queryDmImageList", method = RequestMethod.POST)
    public List<DmBaseImage> queryDmImageList(@RequestParam("targetId") Long targetId,
                                          @RequestParam("type") Integer type,
                                          @RequestParam("category") Integer category) throws Exception {
        Map<String, Object> imageParam = new HashMap<String, Object>();
        imageParam.put("targetId", targetId);
        imageParam.put("type", type);
        imageParam.put("category", category);
        List<DmBaseImage> dmImageList = dmBaseImageMapper.getDmBaseImageListByMap(imageParam);
        return setDefaultImageList(dmImageList, category, type);
    }

    /**
     * 设置图片为空时默认显示图片
     * @param dmImageList
     * @param category
     * @param type
     * @return
     */
    public List<DmBaseImage> setDefaultImageList(List<DmBaseImage> dmImageList, Integer category, Integer type) {
        if (EmptyUtils.isEmpty(dmImageList)) {
            dmImageList = new ArrayList<DmBaseImage>();
            DmBaseImage dmImage = new DmBaseImage();
            dmImage.setType(type);
            dmImage.setCategory(category);
            dmImageList.add(dmImage);
        }
        for(DmBaseImage dmImage:dmImageList){
            if (EmptyUtils.isEmpty(dmImage.getImgUrl())) {
                dmImage.setImgUrl(Constants.DEFAULT_CAROUSEL);
            }
        }
        return dmImageList;
    }



}
